#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, char const *argv[])
{
  // 发送数据打包
  uint8_t send_buff[100] = {0};

  int send_num = snprintf((char*)send_buff, 100, "T=27.65D=-0.00\r\nT=27.65D=-0.00\r\nT=27.65D=-0.01\r\nT=27.65D=-0.00\r\n");
  if (send_num >= 0) {
    if (send_num >= 100) {
      send_num = 100;
      send_buff[send_num-1] = '\0';
    } else {
      send_num += 1;
    }
    // printf("send_num: %d\n", send_num);
    // printf("send_buff: \n%s", send_buff);
  }

  char r_bytes;
  int r_count = 0;
  char r_buff[300] = {0};

  for (int i = 0; i < send_num; i++) {
    r_bytes = send_buff[i];

    if (r_bytes == 'T') {
      r_buff[r_count] = r_bytes;
      r_count++;
    } else if (r_count != 0) {
      r_buff[r_count] = r_bytes;
      r_count++;
    }

    if (r_bytes == '\n') {
      r_buff[r_count] = '\0';
      printf("r_buff: %s\n", r_buff);

      char temp[10] = {0};
      char deep[10] = {0};
      float temp_f = 0.0;
      float deep_f = 0.0;
      int ret = sscanf(r_buff, "T=%[^D]D=%[^\r]\r\n", temp, deep);
      if (ret > 0) {
        printf("temp: %s\n", temp);
        printf("deep: %s\n", deep);
        temp_f = atof(temp);
        deep_f = atof(deep);
        printf("temp_f: %f\n", temp_f);
        printf("deep_f: %f\n", deep_f);
        printf("----------------\n");
      }
      r_count = 0;
    }
    
    if (r_count >= 300) {
      r_count = 0;
    }
    
  }



#if 0
  // int send_num = snprintf((char*)send_buff, 100, "updatez,hello,world,i,am,here,move12,13z");
  int send_num = snprintf((char*)send_buff, 100, "S1111,1,2,1Z");
  if (send_num >= 0) {
    if (send_num >= 100) {
      send_num = 100;
      send_buff[send_num-1] = '\0';
    } else {
      send_num += 1;
    }
    printf("send_num: %d\n", send_num);
    printf("send_buff: %s\n", send_buff);
  }

  char str[4][5] = {0};
  // str  <--> &str[0]   行指针常量
  // str+1 <--> &str[1]
  // str+2 <--> &str[2]
  // str+3 <--> &str[3]
  // which
  // str[0] <--> *str     列指针常量
  // str[1] <--> *(str+1)
  // str[2] <--> *(str+2)
  // str[3] <--> *(str+3)
  sscanf(send_buff,"S%[^,],%[^,],%[^,],%[^Z]Z", *str, *(str+1), *(str+2), *(str+3));
  int info_type = atoi(*str);
  int info_x = atoi(*(str+1));
  int info_y = atoi(*(str+2));
  int info_z = atoi(*(str+3));
  printf("info_type: %d\n", info_type);
  printf("info_x: %d\n", info_x);
  printf("info_y: %d\n", info_y);
  printf("info_z: %d\n", info_z);
#endif

#if 0
  // 接收数据处理
  char str_x[5] = {0};
  char str_y[5] = {0};

  if (strstr((char*)send_buff, "move")) {
    printf("move\n");
    sscanf((char*)send_buff, "%*[^v]ve%2s,%2sz", str_x, str_y);
    int x = atoi(str_x);
    int y = atoi(str_y);
    printf("x: %d, y: %d\n", x, y);
  } else {
    printf("not move\n");
  }

  if (strstr((char*)send_buff, "updatez")) {
    printf("updatez\n");
    memset(send_buff, 0, 100);
  } else {
    printf("not updatez\n");
  }
#endif

  char test_buff[] = "S123456789,2,3,4Z";
  int info_type;
  if (sscanf(test_buff, "S%d,", &info_type) == 1) {
    printf("info_type: %d\n", info_type);
  } else {
    printf("not info_type\n");
  }

  return 0;
}
